Ext.define('Usws.controller.Clz', {
    extend: 'Ext.app.Controller',
    stores:['Clz', 'SchoolEduStage'],
    
    refs: [
   		   {
               ref: 'contentPanel',
               selector: 'contentPanel'
           }
    ],

    init: function() {
        this.control({
            'Clz-Grid': {
                itemdblclick: this.editClz,
                add: this.loadClz
            },

            'Clz-Grid button[action=deleteMore]':{  
                click: this.deleteClzMore
            },
            
            'Clz-Edit button[action=save]': {
                click: this.updateClz
            },
            'Clz-Grid actioncolumn':{
            	itemclick: this.actionColumnOptClz
             }
        });
    },
    
    loadClz:function(obj, component, index, eOpts){
    	var clzStore = this.getClzStore();
    	clzStore.load();
    },
    
    editClz: function(grid, record) {
    	 var view = Ext.widget('Clz-Edit');
    	 Ext.getCmp('clzUid').setValue(record.get('uid'));
    	 Ext.getCmp('name').setValue(record.get('schoolName'));
    	 Ext.getCmp('code').setValue(record.get('schoolCode'));
    	 var schoolEduStageStore = this.getSchoolEduStageStore();
	   	 var schoolUid = record.get('schoolUid');
	   	 schoolEduStageStore.load({
	   		  params:{'school.uid':schoolUid}
	   	 });
	   	 var clzGradeCode = record.get('clzGradeCode');
	   	 var eduStage = schoolEduStageStore.findRecord("value", clzGradeCode.substring(0, clzGradeCode.indexOf('_')));
	   	 alert(eduStage);
	   	 Ext.getCmp('schoolEduStage').select(eduStage);
         view.down('form').loadRecord(record);
    },
    
    actionColumnOptClz: function (column, grid, rowIndex, colIndex, item, e){
        var rec = grid.getStore().getAt(rowIndex);
        if (item.action == 'edit') {  
        	 this.editClz(grid, rec);
        } else if (item.action == 'delete') {  
             this.deleteClz(rec);
        }  
    },
    
    reloadClz:function() {
    	var contentPanel = this.getContentPanel();
    	var clzStore = this.getClzStore();
    	contentPanel.removeAll(true);
    	clzStore.load();
        contentPanel.add(Ext.widget('Clz-Grid'));
        if (contentPanel.floating) {
       	 contentPanel.show();
         }
    },
    
    updateClz: function(button) {
    	var win = button.up('window'),
        form   = win.down('form');
    	var rec = form.getRecord();
    	alert(Ext.getDisplayName(rec));
    	var obj = this;
        form.submit({
        	method:'post',
            url: '/clz/ajaxUpdateClz.action',
            success: function(form, action) {
            	 win.close();
	         	showMessage(
	         			'提醒', 
	         			'添加班级成功！', 
	         			Ext.MessageBox.YESNO, 
	         			Ext.create('Usws.model.ButtonText',{yes: '进去班级管理',no:'继续添班级'}), 
	         			function(btn) {
	         				if(btn === 'yes') {
	         					obj.reloadClz();
	         				} 
	         			 }
	         			);
            },
            failure: function(form, action) {
            	Ext.Msg.alert("提示","更新失败，请重试！");  
            }
        });
    },
    
    deleteClzMore: function(button) {
	    var clzGrid = button.ownerCt.ownerCt;  
        var data = clzGrid.getSelectionModel().getSelection();  
        if(data.length == 0){  
            Ext.Msg.alert("提示","您最少要选择一条数据");  
        }else{  
         	this.deleteClz(data);
        }  
     },
     
     deleteClz: function(data) {
    	 var obj = this;
    	 Ext.MessageBox.confirm("注意", "您真的要做删除操作吗?", function (btn) {
    		 if(btn === 'yes') {
    			 var ids = [];
    	    	 Ext.Array.each(data,function(record){  
    	             ids.push(record.get('uid'));  
    	         });  
    	         Ext.Ajax.request({  
    	             url:'/clz/ajaxDeleteClz.action',  
    	             params:{uids:ids.join(",")},  
    	             method:'POST',  
    	             timeout:2000,  
    	             success:function(response,opts){  
    	             	obj.reloadClz();
    	             }  
    	         });  
    		 }
    	 });
     }
});